package io.zcoder.innoway.activities;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import butterknife.BindView;
import butterknife.BindViews;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.zcoder.innoway.R;
import io.zcoder.innoway.commons.Constants;
import io.zcoder.innoway.commons.PrefUtils;
import io.zcoder.innoway.models.Profile;
import io.zcoder.innoway.models.ResponseBase;
import io.zcoder.innoway.models.User;
import io.zcoder.innoway.rest.RestClient;
import io.zcoder.innoway.widgets.TextViewCustom;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class SelectBodyTypeActivity extends BaseActivity {

    public static final int DURATION_TRANSLATION = 600;
    public static final int TYPE_BODY_1 = 0;
    public static final int TYPE_BODY_2 = 1;
    public static final int TYPE_BODY_3 = 2;

    @BindView(R.id.layout_body_type)
    RelativeLayout mLayoutBodyType;
    @BindViews({R.id.img_body_type_1, R.id.img_body_type_2, R.id.img_body_type_3})
    ImageView[] mBodyType;
    @BindViews({R.id.text_body_type_1, R.id.text_body_type_2, R.id.text_body_type_3})
    TextViewCustom[] mTextBodyType;
    @BindViews({R.id.indicator_1, R.id.indicator_2, R.id.indicator_3})
    View[] mIndicatorViews;

    @BindView(R.id.next)
    TextViewCustom mNext;

    @OnClick(R.id.img_body_type_1)
    void clickBody1() {
        translation(TYPE_BODY_1);
    }

    @OnClick(R.id.img_body_type_2)
    void clickBody2() {
        translation(TYPE_BODY_2);
    }

    @OnClick(R.id.img_body_type_3)
    void clickBody3() {
        translation(TYPE_BODY_3);
    }

    @OnClick(R.id.next)
    void onNext() {
        if (mEditInfo) {
            saveEditInfo();
            return;
        }
        Intent intent = new Intent(this, SelectBirthDayActivity.class);
        mUser.setBody_type(mCurrentType);
        intent.putExtra(Constants.PROFILE_EXTRAS, mUser);
        startActivity(intent);
    }

    private User mUser;
    private int mCurrentType = TYPE_BODY_2;
    private float[] mRatio;
    private boolean mEditInfo;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_select_body_type_activity);
        ButterKnife.bind(this);
        showBack(true);
        mEditInfo = getIntent().getBooleanExtra(Constants.EDIT_INFO_EXTRAS, false);
        if (mEditInfo) {
            mNext.setText(getString(R.string.save));
            Profile profile = getIntent().getParcelableExtra(Constants.PROFILE_EXTRAS);
            initViews(profile.getGender() == 0);
            if (profile.getBody_type() != mCurrentType) {
                translation(profile.getBody_type());
            }

            return;
        }
        mUser = getIntent().getParcelableExtra(Constants.PROFILE_EXTRAS);
        if (mUser == null) {
            return;
        }
        initViews(mUser.isGender());

    }

    private void initViews(boolean male) {
        if (male) {
            mRatio = Constants.RATIO_BODY_TYPE_MAN;
            mBodyType[0].setImageResource(R.drawable.body_type_man_ecto);
            mBodyType[1].setImageResource(R.drawable.body_type_man_meso);
            mBodyType[2].setImageResource(R.drawable.body_type_man_endo);
        } else {
            mRatio = Constants.RATIO_BODY_TYPE_WOMAN;
            mBodyType[0].setImageResource(R.drawable.body_type_woman_ecto);
            mBodyType[1].setImageResource(R.drawable.body_type_woman_meso);
            mBodyType[2].setImageResource(R.drawable.body_type_woman_endo);
        }
    }

    private void translation(int type) {
        if (mCurrentType == type) {
            return;
        }
        float dx = getResources().getDimension(R.dimen.margin_body_type) + getResources().getDimension(R.dimen.padding_body_type) * 2 +
                getResources().getDimension(R.dimen.height_body_type) / mRatio[TYPE_BODY_2] / 2 +
                getResources().getDimension(R.dimen.height_body_type) / mRatio[type] / 2;
        if (type != TYPE_BODY_2) {
            dx = (mCurrentType > type ? 1 : -1) * dx;
            mLayoutBodyType.animate().setDuration(DURATION_TRANSLATION).translationX(dx).start();
        } else {
            mLayoutBodyType.animate().setDuration(DURATION_TRANSLATION).translationX(0).start();
        }
        for (int i = 0; i < 3; i++) {
            if (i == type) {
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).alpha(1f);
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).scaleX(1.15f);
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).scaleY(1.15f);
                mTextBodyType[i].setTextColor(getResources().getColor(R.color.bg_select_tall_btn));
                mIndicatorViews[i].setBackgroundResource(R.drawable.circle_blue);
            } else {
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).alpha(0.3f);
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).scaleX(1f);
                mBodyType[i].animate().setDuration(DURATION_TRANSLATION).scaleY(1f);
                mTextBodyType[i].setTextColor(Color.WHITE);
                mIndicatorViews[i].setBackgroundResource(R.drawable.circle_gray);
            }
        }
        mCurrentType = type;
    }

    private void saveEditInfo() {
        showProgress();
        RestClient.build(this, true).saveBodyType(PrefUtils.getInt(this, PrefUtils.PREF_PROFILE_ID), mCurrentType, new Callback<ResponseBase>() {
            @Override
            public void success(ResponseBase responseBase, Response response) {
                hideProgress();
                if (responseBase.getStatus_code() != 200) {
                    showAlert(getString(R.string.error), responseBase.getMessage());
                } else {
                    finish();
                }
            }

            @Override
            public void failure(RetrofitError error) {
                hideProgress();
            }
        });
    }
}
